-- card: 5632 from stack: in -- bmap block id: 0 -- flags: 0000 -- background id: 2253 -- name: hp2pictXCMD.c -- part contents for background part 1 ----- text ----- hp2pictXCMD.c -- part contents for background part 6 ----- text ----- /* hp2pictXCMD.c * * * Copyright 1990 by d.g.gilbert. * dogStar Software && Indiana University Biology Dept. * email: gilbertd@iubio.bio.indiana.edu * * Language: MPW-C 3.0 * * based in part on AuxWindow by Roger Brown, Dartmouth XCMD's * */ /* Creates a floating, no go-away window with a picture that is translated from HPGL graphics script Card syntax is: hp2pict "Open", name, [rect] -- open blank window * returns window pointer as The Result hp2pict "DrawF", window pointer, FILE w/ HPGL hp2pict "DrawC", window pointer, CONTAINER w/ HPGL -- draw hp contents to window hp2pict "Fetch", window pointer, Container w/ (partial) HPGL -- draw hp while reading it from comm link thru callback to HyperCard, using HC function "recvTo(,,,)" hp2pict "File",window pointer,filename -- save pict as file hp2pict "Clip",window pointer -- put window pict into clipbd hp2pict "Print",window pointer -- print window pict hp2pict "Close",window pointer -- close & dispose of window */ /*-------- Makefile C -b hp2pictXCMD.c -mbg off Link -w -rt XCMD=10003 ∂ -m ENTRYPOINT ∂ -sg hp2pict ∂ hp2pictXCMD.c.o ∂ "{Libraries}HyperXLib.o" ∂ "{Libraries}Interface.o" ∂ "{CLibraries}StdCLib.o" ∂ "{Clibraries}"CSANELib.o ∂ "{Clibraries}"Cinterface.o ∂ "{CLibraries}CRuntime.o" ∂ -o "hp2pict deck" #-------------------------*/ /*#include */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* draw formats */ #define DFILE 1 #define DCARD 2 #define DCOMM 3 void returnValue(XCmdPtr pXCmd, char *theResult); void upcase(char *s); void OpenHPWind(XCmdPtr pXCmd); void DrawHPWind(XCmdPtr pXCmd, int format); void FileHPWind(XCmdPtr pXCmd); void ClipHPWind(XCmdPtr pXCmd); void PrintHPWind(XCmdPtr pXCmd); void CloseHPWind(XCmdPtr pXCmd); /* XCMD entry -- MUST BE FIRST CODE IN SEG */ pascal void EntryPoint(XCmdPtr pXCmd) { Ptr theMessage; char message[32]; GrafPtr cardport; GetPort( &cardport); theMessage = *(pXCmd->params[0]); strcpy(message,theMessage); upcase(message); pXCmd->inArgs[0] = (long) cardport; /* global for subs */ if (strcmp(message,"OPEN")==0) OpenHPWind(pXCmd); else if (strcmp(message,"DRAWF")==0) DrawHPWind(pXCmd, DFILE); else if (strcmp(message,"DRAWC")==0) DrawHPWind(pXCmd, DCARD); else if (strcmp(message,"FETCH")==0) DrawHPWind(pXCmd, DCOMM); else if (strcmp(message,"FILE")==0) FileHPWind(pXCmd); else if (strcmp(message,"CLIP")==0) ClipHPWind(pXCmd); else if (strcmp(message,"PRINT")==0) PrintHPWind(pXCmd); else if (strcmp(message,"CLOSE")==0) CloseHPWind(pXCmd); else { SysBeep(0); returnValue( pXCmd, "Undefined message in hp2pict XCMD"); } SetPort( cardport); return; } #define min(a,b) (ab) ? a : b #define Isalpha(c) ((c >= 'A' & c <= 'Z') \ | (c >= 'a' & c <= 'z')) int Toupper(char c) { return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c ); } void upcase(char *s) { int i,l; l = strlen(s); for (i=0;ileft = Strtod( c, &c); c++; r->top = Strtod( c, &c); c++; r->right = Strtod( c, &c); c++; r->bottom = Strtod( c, &c); } Boolean hasColorQD(void) { SysEnvRec theWorld; int err; err = SysEnvirons(1,&theWorld); if (err!=0) return false; else return theWorld.hasColorQD; } #define lotsOfColor(wind) \ ( (**((CGrafPtr)wind)->portPixMap).pixelSize > 7 ) void returnValue(XCmdPtr pXCmd, char *theResult) { long len; Handle resultHandle; /* return the result to HCard */ len = 1+strlen(theResult); resultHandle = NewHandle(len); HLock(resultHandle); BlockMove(theResult,*resultHandle,len); HUnlock(resultHandle); pXCmd->returnValue = resultHandle; } void OpenHPWind(XCmdPtr pXCmd) { char *theName, *c, theResult[256]; Rect wr; Boolean hascolor; WindowPtr myWind; theName = *(pXCmd->params[1]); hascolor = hasColorQD(); c = *(pXCmd->params[2]); if ((c != NULL) && (*c != 0)) { str2rect( c, &wr); LocalToGlobal( (Point *)&wr.top); LocalToGlobal( (Point *)&wr.bottom); } else SetRect( &wr, 10, 80, 446, 370); /* scaled size for 9" SE monitor */ /*?? use wStorage == newhandle/movehhi instead of newwindow ptr */ if (hascolor) myWind = newcwindow( NULL, &wr, theName, true, noGrowDocProc, (WindowPtr)-1, false, 0); else myWind = newwindow( NULL, &wr, theName, true, noGrowDocProc, (WindowPtr)-1, false, 0); SetPort(myWind); numtostring( myWind,theResult); returnValue( pXCmd, theResult); ShowWindow( myWind); BringToFront( myWind); SetWRefCon( myWind, 0L); SetWindowPic( myWind, NULL); } void CloseHPWind(XCmdPtr pXCmd) { char *theWindowPtr; WindowPtr myWind; PicHandle cardPic, pagePic; theWindowPtr = *(pXCmd->params[1]); stringtonum(theWindowPtr,&myWind); cardPic = GetWindowPic(myWind); if (cardPic != 0L) KillPicture(cardPic); pagePic = (PicHandle)GetWRefCon(myWind); if (pagePic != 0L) KillPicture(pagePic); DisposeWindow(myWind); returnValue( pXCmd, "0"); } #include "hp2pict.inc.c" void DrawHPWind(XCmdPtr pXCmd, int format) { char *c, *buf, *fname, *theWindowPtr, theResult[256]; Handle hphand; long i, hplen; WindowPtr myWind; PicHandle cardPic, pagePic; int err; short fref; Rect windrect, pagerect; Boolean alldone; /* end of all plots -> multipage */ theWindowPtr = *(pXCmd->params[1]); stringtonum(theWindowPtr,&myWind); SetPort(myWind); cardPic = GetWindowPic(myWind); if (cardPic != 0L) KillPicture(cardPic); cardPic = 0L; pagePic = (PicHandle)GetWRefCon(myWind); if (pagePic != 0L) KillPicture(pagePic); pagePic = 0L; if (format == DFILE) { /* --- read HP code from a file ---- */ fname = *(pXCmd->params[2]); if (fsopen( fname, 0, &fref) != 0) { strcpy( theResult, "HPDraw, file not found: "); strcat( theResult, fname); returnValue( pXCmd, theResult); return; } err = GetEOF( fref, &hplen); hphand = NewHandle(hplen); HLock( hphand); err = FSRead( fref, &hplen, *hphand); err = FSClose( fref); HUnlock( hphand); SetHandleSize( hphand, hplen); } else { hphand = pXCmd->params[2]; HandToHand( &hphand); hplen = GetHandleSize(hphand); } buf = *hphand; /* fix any crud in container */ /* !! hi ascii is missed or dropped in C comparisons */ for (i = 0, c = buf; i= 27) *c++ = buf[i]; } *c = 0; hplen = c - buf + 1; SetHandleSize( hphand, hplen+1000); /* !! +1000 debug */ /* windrect = HCard scaled size */ windrect = myWind->portRect; cardPic = hp2pict( false, &windrect, hphand, myWind, format == DCOMM, &alldone, pXCmd); /* pagerect = lw scaled-landscape size */ SetRect( &pagerect, 0, 20, 730, 532); /* -- Note: commfetch is false 2nd time thru -- */ pagePic = hp2pict( true, &pagerect, hphand, myWind, false, &alldone, pXCmd); HUnlock( hphand); pXCmd->returnValue = hphand; /* ? return for debugging */ /* DisposHandle(hphand); returnValue( pXCmd, "0"); */ if ((pagePic != 0L) & GetHandleSize((Handle)pagePic) > 0L ) SetWRefCon(myWind,(long)pagePic); else SetWRefCon(myWind,0); if ((cardPic != 0L) & GetHandleSize((Handle)cardPic) > 0L ) { SetWindowPic(myWind,cardPic); BringToFront(myWind); } else SetWindowPic(myWind,NULL); } void FileHPWind(XCmdPtr pXCmd) { #define headsize 512 char head[headsize]; int n, err; short fref; char *fname, *theWindowPtr; WindowPtr myWind; PicHandle pagePic; theWindowPtr = *(pXCmd->params[1]); stringtonum(theWindowPtr,&myWind); SetPort(myWind); fname = *(pXCmd->params[2]); pagePic = (PicHandle)GetWRefCon(myWind); if (pagePic == 0L) pagePic = GetWindowPic(myWind); if (pagePic != 0L) { /* creator 'MDPL' == MacDraw II */ err = create( fname, 0, 'MDPL', 'PICT'); err = fsopen( fname, 0, &fref); err = SetEOF( fref, 0); for (n=0; nparams[1]); stringtonum(theWindowPtr,&myWind); SetPort(myWind); cardPic = GetWindowPic(myWind); /* pagePic = (PicHandle)GetWRefCon(myWind); */ if (cardPic != 0L) { ZeroScrap(); HLock((Handle)cardPic); PutScrap(GetHandleSize((Handle)cardPic), 'PICT', *(Handle)cardPic); HUnlock((Handle)cardPic); } } void PrintHPWind(XCmdPtr pXCmd) { #define rStopPrintDlog 102 /* this is HCard's PrintIdl dlog */ char *theWindowPtr; WindowPtr myWind; GrafPtr oldPort; TPPrPort prPort; TPrStatus prStat; Boolean okay = false; THPrint hprec; PicHandle pagePic; DialogPtr canDlog; Str255 wname; theWindowPtr = *(pXCmd->params[1]); stringtonum(theWindowPtr,&myWind); SetPort(myWind); pagePic = (PicHandle)GetWRefCon(myWind); if (pagePic == 0L) pagePic = GetWindowPic(myWind); returnValue( pXCmd, "0"); if (pagePic != 0L) { GetPort(&oldPort); PrOpen(); if (PrError() == 0) { hprec = (THPrint) NewHandle(sizeof(TPrint)); PrintDefault( hprec); PrStlDialog( hprec); /* ?? save hprec b/n calls here ? */ if (! PrJobDialog( hprec)) return; /* cancelled by user */ SendHCMessage(pXCmd, "\pset cursor to watch"); GetWTitle(myWind, wname); ParamText(wname,'','',''); canDlog = GetNewDialog(rStopPrintDlog, NULL, (WindowPtr)-1); DrawDialog(canDlog); prPort = PrOpenDoc( hprec, NULL, NULL); SetPort( (GrafPtr)prPort); PrOpenPage(prPort,NULL); DrawPicture( pagePic, &(**pagePic).picFrame); PrClosePage( prPort); PrCloseDoc( prPort); if ( ((**hprec).prJob.bJDocLoop == bSpoolLoop) & (PrError() == 0) ) PrPicFile(hprec, NULL,NULL,NULL, &prStat); if (PrError() == 0) okay = true; DisposHandle( (Handle)hprec); DisposDialog( canDlog); } PrClose(); SetPort(oldPort); } if (!okay) returnValue( pXCmd, "Printing error"); } /* PrintWindow */